<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="关键字" prop="search" label-width="68px">
        <el-input v-model="queryParams.search" placeholder="商品名称、商品编号" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="是否启用" prop="isPutaway">
        <el-select v-model="queryParams.isPutaway" clearable style="width: 240px">
          <el-option v-for="dict in this.status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="createDateRange" style="width: 340px" type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd HH:mm:ss">
          ></el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item> -->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['showroom:art:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :height="tableHeight" ref="elTable" @sort-change="changeTableSort" v-loading="loading" :data="list" :row-key="getRowKeys" @row-dblclick="handleUpdate" stripe border>

      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column label="名称" prop="name" width="200" :sortable="'custom'" />
      <el-table-column label="商品编号" prop="goodsNo" align="center" width="120" :sortable="'custom'" />
      <el-table-column label="商品价(元)" :sortable="'custom'" prop="price" width="120" align="center" />
      <el-table-column label="零售价(元)" :sortable="'custom'" prop="retailPrice" width="120" align="center" />
      <el-table-column label="成本价(元)" :sortable="'custom'" prop="costPrice" width="120" align="center" v-if="checkPermi(['showroom:art:lookprice'])" />
      <el-table-column label="库存" :sortable="'custom'" prop="stock" width="120" align="center" />
      <el-table-column label="启用状态" prop="status" width="120" align="center" :sortable="'custom'">
        <template slot-scope="scope">
          {{selectDictLabel(status,scope.row.status)}}
        </template>
      </el-table-column>
      <el-table-column label="缩略图" prop="imgMin" align="center" width="150">
        <template slot-scope="scope">
          <table-image :imgDicUrls="scope.row.imgMin"></table-image>
        </template>
      </el-table-column>
      <el-table-column label="大图" prop="minList" align="center" width="150">
        <template slot-scope="scope">
          <table-image :imgDicUrls="scope.row.minList"></table-image>
        </template>
      </el-table-column>
      <el-table-column label="排序" sortable prop="orderNum" width="80" align="center" />
      <el-table-column label="创建时间" sortable prop="createTime" align="center" min-width="160" />
      <el-table-column label="操作" fixed="right" align="center" width="180">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isPutaway == 1" size="mini" type="text" icon="" @click.stop="batchUp(scope.row)" v-hasPermi="['showroom:art:batchUp']">下架</el-button>
          <div v-else>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['showroom:art:edit']">编辑</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click.stop="handleDelete(scope.row)" v-hasPermi="['showroom:art:remove']">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 新建 -->
    <div v-if="open">
      <Z-Dialog :title="title" :visible.sync="open" @confirm="submitForm('formView')" @cancel="open=false">
        <form-view :form="form" ref="formView"></form-view>
      </Z-Dialog>
    </div>

  </div>
</template>

<script>
import { listGoodsGiftSend, addGoodGiftSend, editGoodGiftSend, delGoodGiftSend } from "@/api/showroom/goods";
import tableImage from "@/components/TableImageView/index.vue";
import { excelExportApi } from '@/api/filer'
import formView from "./formView.vue";
import { deepClone } from '@/utils/index'
import { checkPermi } from "@/utils/permission"; // 权限判断函数
import hasPer from "@/directive/permission/hasPer.js";

export default {
  name: "goodsGiftSend", //开馆之礼赠品
  components: { tableImage, formView },

  data() {
    return {
      //是否显示止售日期搜索框
      isShowDate: false,
      reSelect: true,
      //上传的图片
      dialogImageUrl: "",
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      list: [], //商品列表

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
      },
      pageNum: 1,
      // 表单参数
      form: {},

      status: [
        { label: "全部", value: null },
        { label: "启用", value: 1 },
        { label: "不启用", value: 0 },

      ]
    };
  },
  watch: {
    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {
        console.log(newV.pageSize, this.queryParams.pageSize)
        if (this.pageNum == newV.pageNum) {
          this.handleQuery();
        } else {
          this.pageNum = newV.pageNum;
        }
      },
    },
    createDateRange(newV, oldV) {
      if (newV) {
        if (newV.length >= 2) {
          this.queryParams.createTimeStart = String(newV[0]);
          this.queryParams.createTimeEnd = String(newV[1]);
        }
      } else {
        this.queryParams.createTimeStart = undefined;
        this.queryParams.createTimeEnd = undefined;
      }
      this.getList();
    },
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 280px)'
    },
  },
  created() {
    this.getList();
  },
  methods: {
    checkPermi,
    getList() {
      this.loading = true;
      listGoodsGiftSend(this.queryParams)
        .then((response) => {
          if (response.code == 200) {
            this.list = response.data;
            this.total = response.total;
          }
          this.loading = false;
          this.reSelect = true;
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    changeTableSort(column) {
      this.queryParams.orderBy = column.prop;
      this.queryParams.isAsc = (column.order == 'descending' ? 0 : 1); //ascending == 升序 descending == 降序  0 倒,1正    
      this.getList();
    },

    getRowKeys(row) {
      return row.id
    },

    //下架
    batchUp(row) {
      this.batchUpParams.ids = [row.id];
      this.batchUpParams.isPutaway = 0;
      this.$modal
        .confirm('是否确认下架商品编号"' + row.goodsNo + '的商品"？')
        .then(() => {
          this.changeLotStatus();
        }
        );
    },

    /**一键上下架 */
    changeLotStatus() {
      if (!this.batchUpParams.ids) {
        this.$modal.msgError("请选择需要操作的商品");
        return;
      }
      batchUpGoodsInfo(this.batchUpParams).then((response) => {
        this.$modal.msgSuccess("操作成功");
        this.queryParams.isPutaway = undefined;
        this.batchUpParams.ids = [];
        this.$refs.elTable.clearSelection();
        this.reSelect = false;
        this.getList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      // this.resetForm("form");
      this.form = {};
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.endPrice = undefined;
      this.queryParams.createDateRange = undefined;
      this.queryParams.createTimeStart = undefined;
      this.createDateRange = undefined;
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },

    /**上传数据 */
    uploaded(params, dict) {
      this.form[params] = JSON.stringify(dict);
    },

    // clickRow(row) {
    //   console.log(row);
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // if (!hasPer.isPerShow(['showroom:art:edit'])) {
      //   return;
      // }
      this.reset();
      this.title = "编辑";
      this.form = deepClone(row);
      this.open = true;
    },

    /** 提交按钮 */
    submitForm(refStr) {
      let ref = this.$refs[refStr];
      let valueData = ref.$refs.editor.currentValue;
      this.form.details = valueData;
      this.form.payType = 0;
      this.form.imgMin = this.form.minList;
      this.form.imgMax = this.form.minList;

      ref.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            editGoodGiftSend(this.form).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoodGiftSend(this.form).then((response) => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除编号为"' + row.goodsNo + '"的数据项？')
        .then(() => {
          delGoodGiftSend({ id: row.id }).then((response) => {
            this.$modal.msgSuccess("删除成功");
            this.getList();
          });
        }
        ).catch(() => { });
    },

    /** 导出按钮操作 */
    // exportExcel() {
    //   this.queryParams.beanName = 'GoodsInfoExport',
    //     this.queryParams.fileName = '竞价品',
    //     this.$alert('需要导出 "' + this.total + '" 条数据,是否确认导出', '提示', {
    //       confirmButtonText: '确定',

    //     }).then(() => {
    //       excelExportApi(this.queryParams, '/pms');
    //     });
    // },
  },
};
</script>
<style scoped>
.el-dialog-div {
  height: 60vh;
  overflow: auto;
}
.input-W {
  width: 100%;
}
</style>
